package cn.liyicun.test.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.system.SystemUtil;
import cn.liyicun.test.base.Common;
import cn.liyicun.test.service.UserFileService;
import cn.liyicun.test.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class IndexController{

    @Value("${server.port:8080}")
    int port;

    @Autowired
    UserService user;
    @Autowired
    UserFileService ufile;

    @RequestMapping("/")
    public String home(HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {


        HttpSession session = req.getSession(false);
        if(session!=null){
            if(session.getAttribute(Common.USER_SESSION_NAME)!=null){
                res.sendRedirect("main");return null;
            }
        }

        model.addAttribute("file_count", ufile.countFile()*7);
        model.addAttribute("user_count", user.countUser()*13);
        return "index";
    }

    @RequestMapping("/sysInfo")
    public String sysInfo(HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {

        String ip = ServletUtil.getClientIP(req);
        log.info(ip);

        model.addAttribute("getHostInfo", SystemUtil.getHostInfo());
        model.addAttribute("getJvmInfo", SystemUtil.getJvmInfo());
        model.addAttribute("getUserInfo", SystemUtil.getUserInfo());
        model.addAttribute("getJvmSpecInfo", SystemUtil.getJvmSpecInfo());
        model.addAttribute("getOsInfo", SystemUtil.getOsInfo());
        model.addAttribute("getRuntimeInfo", SystemUtil.getRuntimeInfo());
        model.addAttribute("server_port", port);

        return "index";
    }

}